package com.demo.lock.jdk;

import java.util.concurrent.locks.ReentrantLock;

public class FairLock implements Runnable {

    public static ReentrantLock lock = new ReentrantLock(true);

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "获取到锁");
            } catch (Exception e) {

            } finally {
                lock.unlock();
            }
        }
    }


    public static void main(String[] args) {
        FairLock lock = new FairLock();

        Thread t1 = new Thread(lock, "thread1");
        Thread t2 = new Thread(lock, "thread2");
        Thread t3 = new Thread(lock, "thread3");

        t1.start();
        t2.start();
        t3.start();
    }
}
